package com.flashflexpro.flexdirector.model{
    import com.flashflexpro.as3lib.EventDictionary;

    import flash.events.Event;

    import mx.collections.ArrayCollection;

    [Bindable]
    [RemoteClass]
    public class DtoApp extends DtoReusable{

        public function DtoApp(){
            name = "Unnamed App";
        }

        public var viewColor:int = 0x000000;

        public var url:String = "http://localhost:8089/aaa.swf";

        public var environment:XML = null;
        public var isStandardSparkEnv:Boolean = false;
        public var isStandardHaloEnv:Boolean = false;

        public var appRoot:DtoFlexComInst;

        public var classTree:ClassDesc;


        public var mainSuperClassNames:Array;

        override public function toString():String{
            return name + "->" + url;
        }

        public function parseAutoId( aId:String ):DtoFlexComInst{
            var insts:Array = aId.split( "|" );
            var len:int = insts.length;
            var currentParent:DtoFlexComInst = null;
            var rt:DtoFlexComInst;
            godeep:
                    for( var i:int = 0; i < len; i++ ){
                        rt = getFlexComInstByOrgAId( insts[i] as String );
                        if( currentParent != null ){
                            if( currentParent.children != null ){
                                for( var j:int = currentParent.children.length - 1; j >= 0; j-- ){
                                    var currentComInst:DtoFlexComInst = currentParent.children.getItemAt( j ) as
                                            DtoFlexComInst;
                                    CONFIG::debugging{
                                        if( currentComInst == null ){
                                            throw new Error( this + ".parseAutoId currentComInst can't be null" );
                                        }
                                    }
                                    if( rt.equals( currentComInst ) ){
                                        currentParent = currentComInst;
                                        continue godeep;
                                    }
                                }
                            }
                            else{
                                currentParent.children = new ArrayCollection();
                            }
                            rt.parent = currentParent;
                            currentParent.children.addItem( rt );
                            currentParent = rt;
                        }
                        else{
                            if( i == 0 ){
                                if( appRoot == null ){
                                    appRoot = rt;
                                }
                                currentParent = appRoot;
                            }
                            else{
                                if( appRoot == null ){
                                    throw new Error( this + ".parseAutoId never happend!" );
                                }
                            }
                        }
                    }
            return rt;
        }


        private function getFlexComInstByOrgAId( orgAId:String ):DtoFlexComInst{
            var rt:DtoFlexComInst = new DtoFlexComInst();
            var arr:Array = orgAId.split( "}" );
            for( var i:int = arr.length - 1; i >= 0; i-- ){
                var s:String = arr[i] as String;
                if( s == "" ){
                    continue;
                }
                if( s.indexOf( "automationName" ) == 0 ){
                    rt.automationName = getPropertyValue( s );
                } else if( s.indexOf( "automationIndex" ) == 0 ){
                    rt.automationIndex = getPropertyValue( s );

                } else if( s.indexOf( "automationClassName" ) == 0 ){
                    rt.automationClassName = getPropertyValue( s );

                } else if( s.indexOf( "className" ) == 0 ){
                    rt.className = getPropertyValue( s );

                } else if( s.indexOf( "id" ) == 0 ){
                    rt.instId = getPropertyValue( s );
                }
                else{
                    var otherName:String = s.split( "{" )[ 0 ];
                    CONFIG::debugging{
                        if( otherName == null ){
                            throw new Error( "DtoFlexCompinst.getFlexComInstByOrgAId How can property name is null!" );
                        }
                    }
                    if( rt.otherData == null ){
                        rt.otherData = new EventDictionary();
                    }
                    rt.otherData[ otherName ] = getPropertyValue( s );
                }
            }
            return rt;
        }

        private static function getPropertyValue( s:String ):String{
            return s.split( "{" )[ 1 ] as String;
        }

        /*

         automationName{index:-1 string}
         automationIndex{index:-1 string}
         automationClassName{FlexApplication string}
         className{AAA string}
         id{null object}
         label{ string}
         |
         automationName{text string}
         automationClassName{FlexBox string}
         automationIndex{index:3 string}
         className{mx.containers.VBox string}
         id{txtWithNewId string}
         label{text string}
         |
         automationName{changedTxtId string}
         automationClassName{FlexPanel string}
         automationIndex{index:2 string}
         id{changedTxtId string}
         label{ string}
         className{mx.controls.RichTextEditor string}
         |
         automationName{textArea string}
         automationIndex{index:0 string}
         automationClassName{FlexTextArea string}
         id{textArea string}
         className{mx.controls.TextArea string}
         editable{true boolean}
         displayAsPassword{false boolean}" );
         * */

    }
}